// protocol
// ============

define(['zepto', 'utils', 'JST'], function($, utils, JST){
  return {
    init: function(_CTX_, link, type, isOnlineYear) {
      this._CTX_ = _CTX_;
      this.link = link;
      this.type = type;
      this.isOnlineYear = isOnlineYear
      this.$bindButton = $('[href="#protocol-push"]');
      this.$bindButton.length ? this.bindProtocolAction() : void 0;
    },

    /**
     * Bind action fired on clicking show protocol doc button
     */
    bindProtocolAction: function() {
      var that = this

      this.$bindButton.on('click', function(e) {
        e.preventDefault();
        
        if(that.type == 'online') {
        
          if(that.isOnlineYear == 'true') {
            var $page = $(JST.onlineYearProtocolTpl({ 
              'link': that.link,
              'resource': that._CTX_.resource
            }));
          } else {
            var $page = $(JST.onlineProtocolTpl({ 
              'link': that.link,
              'resource': that._CTX_.resource
            }));
          }

          that.bindProtocolAgreeButton($page);
          $.push.transitionIn($page);

        } else if (that.type == 'offline' || that.type == 'year') {

          var $page = $(JST.offlineProtocolTpl({ 
            'link': that.link,
            'resource': that._CTX_.resource
          }));
          
          that.bindProtocolAgreeButton($page);
          $.push.transitionIn($page);
        }
        
      });
    },

    bindProtocolAgreeButton: function($page) {

      var $button  = $("a.btn", $page),
          $time    = $("a.btn span", $page),
          duration = 5;

      var countdown = setInterval(function(){
        if (--duration) {
          $($time).text(duration);
        // Otherwise
        } else {

          // Clear the countdown interval
          clearInterval(countdown)
          // And fire the callback passing our container as `this`
          $($time).text('');
          $($button).removeClass('btn-disabled');
          $($button).attr('href', $($button).data("href"));
        }
   　 }, 1000);
    }
  }

})

